/******************************************************************************
 Copyright (c) 2022-2025 Qualcomm Technologies International, Ltd.
 All Rights Reserved.
 Qualcomm Technologies International, Ltd. Confidential and Proprietary.

 REVISION:      $Revision: #15 $
******************************************************************************/

#include "csr_synergy.h"
#include "gatt_lib.h"

#ifndef EXCLUDE_GATT_XPAN_SERVER_MODULE

#include "gatt_xpan_server_msg_handler.h"

/**************************************************************************/
static void xpanServerMsgHandler(void* task, MsgId id, Msg msg)
{
    GattXpanServerData *gattXpanServerInstance = (GattXpanServerData *)task;

    switch(id)
    {
        case CSR_BT_GATT_REGISTER_CFM:
        {
            GATT_XPAN_SERVER_INFO(" \n XPAN Server: CSR_BT_GATT_REGISTER_CFM receivced \n");

            CsrBtGattRegisterCfm* cfm = (CsrBtGattRegisterCfm*)msg;
            if (cfm->resultCode == CSR_BT_GATT_RESULT_SUCCESS &&
                    cfm->resultSupplier == CSR_BT_SUPPLIER_GATT)
            {
                gattXpanServerInstance->gattId = cfm->gattId;

                CsrBtGattFlatDbRegisterHandleRangeReqSend(gattXpanServerInstance->gattId,
                                                          gattXpanServerInstance->startHandle,
                                                          gattXpanServerInstance->endHandle);
            }
            break;
        }
        case CSR_BT_GATT_DB_ACCESS_WRITE_IND:
        {
            GATT_XPAN_SERVER_INFO(" \n XPAN Server: CSR_BT_GATT_DB_ACCESS_WRITE_IND received \n");
            xpanServerHandleAccessWriteIndication(gattXpanServerInstance, (CsrBtGattDbAccessWriteInd*)msg);
            break;
        }
        case CSR_BT_GATT_DB_ACCESS_READ_IND:
        {
            GATT_XPAN_SERVER_INFO(" \n XPAN Server: CSR_BT_GATT_DB_ACCESS_READ_IND received \n");
            xpanServerHandleAccessReadIndication(gattXpanServerInstance, (CsrBtGattDbAccessReadInd *)msg);
            break;
        }

        case CSR_BT_GATT_UNREGISTER_CFM:
        {
            GATT_XPAN_SERVER_INFO(" \n XPAN Server: CSR_BT_GATT_UNREGISTER_CFM received \n");
            CsrBtGattUnregisterCfm* cfm = (CsrBtGattUnregisterCfm*)msg;
            if (cfm->resultCode == CSR_BT_GATT_RESULT_SUCCESS &&
                    cfm->resultSupplier == CSR_BT_SUPPLIER_GATT)
            {
                ServiceHandleFreeInstanceData(gattXpanServerInstance->serviceHandle);
            }
            break;
        }

        default:
        {
            GATT_XPAN_SERVER_WARNING(
                        "XPAN: GATT Manager message 0x%04x not handled\n",
                        id
                        );
            break;
        }

    }/* switch */

    CsrBtGattFreeUpstreamMessageContents(CSR_BT_GATT_PRIM, msg);
}

void GattXpanServerMsgHandler(void** gash)
{
    uint16 eventClass = 0;
    void* msg = NULL;
    ServiceHandle srvcHndl = 0;

    GattXpanServerData* xpan = NULL;

    srvcHndl = *((ServiceHandle*)*gash);
    xpan = ServiceHandleGetInstanceData(srvcHndl);

    if (xpan == NULL)
    {
        GATT_XPAN_SERVER_ERROR("/nXPAN: is NULL/n");
        return;
    }

    if (CsrSchedMessageGet(&eventClass, &msg))
    {
        switch (eventClass)
        {
            case CSR_BT_GATT_PRIM:
            {
                CsrBtGattPrim id;
                id = *((CsrBtGattPrim*)msg);
                xpanServerMsgHandler(xpan, id, msg);
                break;
            }
            default:
                break;
        }

        SynergyMessageFree(eventClass, msg);
    }
}

#endif /* EXCLUDE_GATT_XPAN_SERVER_MODULE */
